////////////////////////////////////////////////////////////////////////////////
//
//  MICROSOFT CORPORATION
//  Copyright (c) Microsoft Corporation.
//  All Rights Reserved.
//
//  NOTICE: Microsoft Confidential. Intended for Internal Use Only.
//
////////////////////////////////////////////////////////////////////////////////

package com.msn.beet.logging
{

import com.msn.beet.events.LocalConnectionEvent;
import com.msn.beet.logging.ILogHandler;
import com.msn.beet.logging.Message;

import flash.events.AsyncErrorEvent;
import flash.events.IEventDispatcher;
import flash.events.StatusEvent;
import flash.net.LocalConnection;

/**
 * Wrapper for LocalConnection, deconstructs <code>Message</code> and sends
 * contents to <code>AbstractLocalConnectionClient</code>.
 *
 * <p>Captures error events and error level status events broadcast by
 * LocalConnection and throws them as LocalConnectionError(s) with specific
 * Id and original error or event.</p>
 *
 * @see AbstractLocalConnectionClient
 * @see com.msn.beet.logging.Message
 * @see com.msn.beet.errors.LocalConnectionError
 *
 */
public class LocalConnectionHandler implements ILogHandler, IEventDispatcher
{
    include "../core/Version.as";
    include "../events/EventDispatcherMixin.as";

    public static const LOCAL_CONNECTION_ID:String = "_loggerLocalConnection";
    public static const RECIEVER_FUNCTION:String =  "transmit";

    protected static const STATUS_LEVEL_ERROR:String = "error";

    protected var connection:LocalConnection = new LocalConnection();

    /**
     * Initializes localConnection object for transmitting messages.
     */
    public function LocalConnectionHandler()
    {
        this.connection.addEventListener(AsyncErrorEvent.ASYNC_ERROR,
                                         asynchErrorHandler);
        this.connection.addEventListener(StatusEvent.STATUS,
                                         statusErrorHandler);
    }

    /**
     * Invoked by Log utility to send data to LCReceiver. Extracts data from
     * messages, sends contents to LCReceiver.
     *
     * Dispatches LocalConnectionEvent.MULTIPLE_CONNECTIONS_ERROR
     */
    public function notify(message:Message):void
    {
        var content:Array       = message.content;
        var logLevelName:String = message.logLevel.name;
        var upTime:int          = message.upTime;
        var timeStamp:Date      = message.timeStamp;
        var className:String    = message.className;
        var line:int            = message.line;

        try
        {
            connection.send(LOCAL_CONNECTION_ID, RECIEVER_FUNCTION, content,
                            logLevelName, upTime, timeStamp, className, line);
        }
        catch(e:ArgumentError)
        {
            var msg:String = "Attempting to send over 40k of data in " +
                             "com.msn.beet.logging." +
                             "LocalConnectionHandler#notify";
            this.dispatchEvent(new LocalConnectionEvent(
                LocalConnectionEvent.ARGUMENT_ERROR_EVENT, msg, e))
        }
    }

    /** Dispatches LocalConnectionEvent.STATUS_ERROR         */
    protected function statusErrorHandler(e:StatusEvent):void
    {
        if (e.level == STATUS_LEVEL_ERROR)
        {
            var msg:String = "LocalConnectionError - Error level StatusEvent " +
                             "in com.msn.beet.logging.LocalConnectionHandler" +
                             " check that '" + LOCAL_CONNECTION_ID +
                             "' is started.";

            this.dispatchEvent(new LocalConnectionEvent(
                LocalConnectionEvent.STATUS_ERROR_EVENT, msg, e))
        }
    }

    /** Dispatches LocalConnectionEvent.ASYNCH_ERROR         */
    protected function asynchErrorHandler(e:AsyncErrorEvent):void
    {
        var msg:String = "LocalConnectionError - AsyncErrorEvent in " +
                         "com.msn.beet.logging.LocalConnectionHandler";
        this.dispatchEvent(new LocalConnectionEvent(
            LocalConnectionEvent.ASYNCH_ERROR_EVENT, msg, e))
    }

}

}
